package com.example.seckill.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seckill.domain.UserInfo;
import com.example.seckill.domain.UserPassword;
import com.example.seckill.error.BusinessException;
import com.example.seckill.error.BussinessAssert;
import com.example.seckill.error.CommonErrorEnum;
import com.example.seckill.mapper.UserInfoMapper;
import com.example.seckill.mapper.UserPasswordMapper;
import com.example.seckill.mappstruct.UserMap;
import com.example.seckill.service.UserInfoService;
import com.example.seckill.service.model.UserModel;
import com.example.seckill.validator.ValidateResult;
import com.example.seckill.validator.ValidatorComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;


/**
 *
 */
@SuppressWarnings("ALL")
@Service
@Slf4j
@Validated
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserPasswordMapper userPasswordMapper;
    @Autowired
    private ValidatorComponent validatorComponent;

    @Override
    public UserModel getUserById(Integer id) {
        log.info("根据用户id查找用户");
        UserInfo userInfo = getById(id);
        BussinessAssert.state(userInfo != null, CommonErrorEnum.USER_NOT_EXIST);
        UserPassword userPassword = userPasswordMapper.selectOne(new QueryWrapper<UserPassword>().lambda().eq(UserPassword::getUserId, id));

        UserModel userModel = convertFromUserDo(userInfo, userPassword);
        return userModel;
    }

    @Transactional
    @Override
    public void register(UserModel userModel) {
        //判断
        ValidateResult validateResult = validatorComponent.validate(userModel);
        if (validateResult.getHasError()) {
            log.info("用户注册校验不通过", validateResult.getErrorMsgs());
            throw new BusinessException(CommonErrorEnum.PARAMETER_VALIDATION_ERROR, validateResult.getErrorMsgs());
        }
        //保存用户
        UserInfo userInfo = UserMap.INSTANCE.convertDOFromModel(userModel);
        userInfo.setRegisterMode("byphone");
        this.baseMapper.insert(userInfo);
        //保存密码
        UserPassword userPassword = new UserPassword();
        userPassword.setEncryptPassword(userModel.getEncryptPassword());
        userPassword.setUserId(userInfo.getId());
        userPasswordMapper.insert(userPassword);
    }

    @Override
    public UserModel validateLogin(String telphone, String encryptPassword) {
        UserInfo userInfo = this.baseMapper.selectOne(new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getTelphone, telphone));
        if (userInfo == null) {
            throw new BusinessException(CommonErrorEnum.USER_NOT_EXIST,"手机或密码不正确");
        }
        UserPassword userPassword = userPasswordMapper.selectOne(new QueryWrapper<UserPassword>().lambda().eq(UserPassword::getUserId, userInfo.getId()));
        if(!StrUtil.equals(encryptPassword,userPassword.getEncryptPassword())){
            throw new BusinessException(CommonErrorEnum.USER_NOT_EXIST,"手机或密码不正确");
        }

        return convertFromUserDo(userInfo,userPassword);
    }



    private UserModel convertFromUserDo(UserInfo userInfo, UserPassword userPassword) {
        UserModel userModel = UserMap.INSTANCE.convertFromDataObject(userInfo);
        userModel.setEncryptPassword(userPassword.getEncryptPassword());
        return userModel;
    }


}




